package com.mes.basicdata.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.dao.iface.IDeptDao;
import com.mes.basicdata.dao.iface.IEmployeeDao;
import com.mes.basicdata.domain.Employee;
import com.mes.basicdata.manager.iface.IEmployeeManager;
import com.mes.common.service.BasicService;

@Service
public class EmployeeService extends BasicService<Employee>{

	@Autowired
	private IEmployeeDao employeeDao;
	
	@Autowired
	private IDeptDao deptDao;
	
	@Autowired
	private IEmployeeManager employeeManager;
	
	@DataProvider
	public void getByDeptId(Page<Employee> page, Map<String, Object> map) throws Exception {
		String deptUid = (String)map.get("deptUid");
		if (StringUtils.isNotBlank(deptUid)) {
			employeeDao.getByDeptId(deptUid, page);
		}
		
		
	}
	
	@DataProvider
	public Collection<Employee> getEmployeeByDeptId(String deptId) throws Exception {
		Collection<Employee> employees = new ArrayList<Employee>();
		if (deptId != null && deptId != ""){
			employees = employeeDao.findByProperty("deptUid", deptId);
		}
		return employees;
	}
	
	@DataProvider
	public List<Employee> getEmployeesByRoleUid(Page<Employee> page, String roleUid) throws Exception {
		return employeeDao.getEmployeesByRoleUid(roleUid, page);
	}
	

	/**
	 * 保存Employee及DefaultUser
	 * @param dataItems
	 * @throws Exception
	 */
	@DataResolver
	public void saveEmployee(Map<String, Collection<Employee>> dataItems) throws Exception {
		
		employeeManager.saveEmployee(dataItems);

	}
	 
	@DataProvider
	public Collection<Employee> getCheckersByDeptUid(String deptUid, Criteria criteria) throws Exception {
		return employeeDao.getCheckersByDeptUid(deptUid, criteria);
	}
	
	@DataResolver
	@Override
	public void saveAll(Map<String, Collection> dataItems) throws Exception {
		super.saveAll(dataItems);
		//employeeBiz.testTransaction();
	}
	
	
	@DataProvider
    public void getEmployeeByConditions(Page<Employee> page, Map<String, Object> params){
    	if (params != null && !params.equals("") ){
    		try {
    			employeeDao.getEmployeeByConditions(page, params);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}else {
			try {
				//this.getAll(page);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
	
	@DataProvider
	public Employee getLoginUser(String cardId) throws Exception {
		IUser loginUser = ContextHolder.getLoginUser();
		String id = loginUser.getUsername();
		return employeeDao.getById(id);
	}
	
	@DataProvider
	public Employee getEmployeeBycardId(String cardId) throws Exception {
		List<Employee> employees = employeeDao.findByProperty("cardId", cardId);
		
		Employee employee = null;
		if (employees.size() > 0){
			employee = employees.get(0);
		}
		
		return employee;
	}

}
